#include <bits/stdc++.h>

#include <atcoder/all>
#define LL long long
using namespace std;
using namespace atcoder;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;

  cin >> n >> m;
  scc_graph scc_g(n);
  for (; m; m--) {
    int a, b;
    cin >> a >> b;
    scc_g.add_edge(a, b);
  }
  auto scc_ans = scc_g.scc();
  cout << scc_ans.size() << "\n";
  for (auto& scc : scc_ans) {
    cout << scc.size();
    for (auto vertex : scc) {
      cout << " " << vertex;
    }
    cout << "\n";
  }
  return 0;
}
